/*
 * @Author: your name
 * @Date: 2021-09-02 15:58:19
 * @LastEditTime: 2021-09-14 11:53:54
 * @LastEditors: Please set LastEditors
 * @Description: 维保团队成员相关
 * @FilePath: \weplus_weibao\src\setup\maintenanceteam\MaintenanceTeamMembersSetup.js
 */
import { reactive, getCurrentInstance } from 'vue';
import { apiService } from '../../api/apiService';
import { showError, showSuccess, showServerError } from '../../util/MessageDeal';
import { editEnterpriseMember, getEnterpriseMembers, getEnterpriseMembersByPage, getEnterpriseMember } from '../../api/server';
// import store from '../../store/index';

export default function useMaintenanceMembersSetup () {
    const ctx = getCurrentInstance();
    const $st = ctx.appContext.config.globalProperties.$st;
    const state = reactive({
        tableData: [],
        total: 0
    });
    const sexMap = new Map([
        ['male', $st('Male')], ['female', $st('Female')]
    ]);
    const statusMap = new Map([
        ['0', $st('unknow')], ['1', $st('At_work')], ['2', $st('On_standby')], ['3', $st('Resting')]
    ]);
    // 获取所有维保成员列表
    const getAllMaintenanceTeamMembers = () => {
        return new Promise((resolve, reject) => {
            apiService({
                api: getEnterpriseMembers,
                success: (data) => {
                    // data.value;
                    // data.allnum;
                    resolve(data);
                },
                fail: (data) => {
                    showServerError(data);
                    reject(data);
                }
            });
        });
    };
    // 根据分页和过滤条件获取所有维保成员详细信息
    const getAllTeamMembersDetailByPage = (n, page, search) => {
        return new Promise((resolve, reject) => {
            apiService({
                api: getEnterpriseMembersByPage,
                param: {
                    n: n,
                    page: page,
                    ...search
                },
                success: (data) => {
                    state.tableData = data.value;
                    state.total = data.allnum;
                    for (var i = 0; i < state.tableData.length; i++) {
                        state.tableData[i].sex = sexMap.get(state.tableData[i].sex) || state.tableData[i].sex;
                        state.tableData[i].status = statusMap.get(state.tableData[i].status) || state.tableData[i].status;
                    }
                    // for (var i = 4; i <= 25; i++) {
                    //     const str = i + '' + i + i + '';
                    //     state.tableData.push({
                    //         account: str,
                    //         alias: str,
                    //         sex: 'male',
                    //         phone: str,
                    //         customer_name: 'abcd',
                    //         address: str,
                    //         status: str
                    //     });
                    // }
                    // state.total += 22;

                    resolve(data);
                },
                fail: (data) => {
                    showServerError(data);
                    reject(data);
                }
            });
        });
    };
    // 编辑维保成员详情
    const editMemberDetails = (form) => {
        return new Promise((resolve, reject) => {
            apiService({
                api: editEnterpriseMember,
                param: {
                    account: form.account,
                    alias: form.alias,
                    sex: form.sex,
                    phone: form.phone,
                    customer_id: form.customer_id,
                    customer_name: form.customer_name,
                    address: form.address,
                    location: form.location,
                    status: form.status
                },
                success: () => {
                    showSuccess($st('op_success'));
                },
                fail: () => {
                    showError($st('op_fail'));
                }
            });
        });
    };

    // 获取某个维保团队成员信息
    const getOneMemberInfo = (account) => {
        return new Promise((resolve, reject) => {
            apiService({
                api: getEnterpriseMember,
                param: {
                    account: account
                },
                success: (data) => {
                    resolve(data);
                    // showSuccess($st('op_success'));
                },
                fail: (data) => {
                    showServerError(data);
                    reject(data);
                }
            });
        });
    };

    return {
        getOneMemberInfo,
        editMemberDetails,
        getAllMaintenanceTeamMembers,
        getAllTeamMembersDetailByPage,
        state
    };
}
